/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * XMLArgTrustUIFRame.java
 *
 * Created on May 20, 2011, 2:59:15 PM
 */
package argtrust.apiexamples;

import argtrust.cmd.ArgTrust;
import javax.swing.JFileChooser;
import java.io.File;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
import java.awt.Color;
import java.awt.Dimension;

/**
 *
 * @author Yuqing Tang
 */
public class XMLArgTrustUIFRame extends javax.swing.JFrame {

    /** Creates new form XMLArgTrustUIFRame */
    public XMLArgTrustUIFRame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mButtonOpenXML = new javax.swing.JButton();
        mButtonDraw = new javax.swing.JButton();
        canvas1 = new java.awt.Canvas();
        mDrawPanel = new java.awt.ScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        mButtonOpenXML.setText("Open XML");
        mButtonOpenXML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mButtonOpenXMLActionPerformed(evt);
            }
        });

        mButtonDraw.setText("Draw");
        mButtonDraw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mButtonDrawActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(mButtonOpenXML)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 147, Short.MAX_VALUE)
                .addComponent(mButtonDraw)
                .addGap(101, 101, 101))
            .addGroup(layout.createSequentialGroup()
                .addGap(408, 408, 408)
                .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(mDrawPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mButtonOpenXML)
                    .addComponent(mButtonDraw))
                .addGap(1, 1, 1)
                .addComponent(mDrawPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(72, 72, 72))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mButtonOpenXMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mButtonOpenXMLActionPerformed
        final JFileChooser fc = new JFileChooser();

        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            mCurrentDirectory = fc.getCurrentDirectory().getPath();
            mInputFileName = fc.getSelectedFile().getName();
            ArgTrust arg_trust = new ArgTrust();
            String output_file = mCurrentDirectory + File.separatorChar + mInputFileName + ".dot";
//            arg_trust.run(args);
        }

    }//GEN-LAST:event_mButtonOpenXMLActionPerformed

    private void mButtonDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mButtonDrawActionPerformed


        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try {
            graph.enterGroup();
            Object v1 = graph.insertVertex(graph.getDefaultParent(), null, "Hello1", 20, 20, 200,
                    130);
            graph.updateCellSize(v1);
            Object v2 = graph.insertVertex(graph.getDefaultParent(), null, "World1!", 240, 150,
                    80, 30);
            graph.updateCellSize(v2);
            graph.insertEdge(graph.getDefaultParent(), null, "Edge1", v1, v2);
            graph.exitGroup();

            graph.enterGroup();
            Object vv1 = graph.insertVertex(graph.getDefaultParent(), null, "Hello", 20, 20, 80,
                    30);
            graph.updateCellSize(vv1);
            Object vv2 = graph.insertVertex(graph.getDefaultParent(), null, "World!", 240, 80,
                    80, 30);
            graph.updateCellSize(vv2);
            graph.insertEdge(graph.getDefaultParent(), null, "Edge2", vv1, vv2);
            graph.exitGroup();
//            graph.groupCells(sub_graph);
//            graph.groupCells(sub_graph0);


            graph.insertEdge(parent, null, "Edge", v1, vv2);


//            cells = graph.getChildCells(parent);
//            for (int i = 0; i < cells.length; i++) {
//                graph.updateCellSize(cells[i], false);
//            }


            com.mxgraph.layout.mxIGraphLayout layout = new com.mxgraph.layout.mxCircleLayout(graph);
            layout.execute(parent);
            new com.mxgraph.layout.mxPartitionLayout(graph).execute(parent);


        } finally {
            graph.getModel().endUpdate();
        }

        mxGraphComponent graphComponent = new mxGraphComponent(graph);
//        mPanelDraw.setBackground(Color.white);
//        mPanelDraw.setPreferredSize(new Dimension(400, 0));
        this.mDrawPanel.add(graphComponent);
  
    }//GEN-LAST:event_mButtonDrawActionPerformed
    String mCurrentDirectory;
    String mInputFileName;

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                XMLArgTrustUIFRame ui_frame = new XMLArgTrustUIFRame();
                ui_frame.setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Canvas canvas1;
    private javax.swing.JButton mButtonDraw;
    private javax.swing.JButton mButtonOpenXML;
    private java.awt.ScrollPane mDrawPanel;
    // End of variables declaration//GEN-END:variables
}
